package com.wh.hotel.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wh.common.annotation.ResultLog;
import com.wh.common.enums.MethodTypeEnum;
import com.wh.hotel.constants.CommonConstant;
import com.wh.hotel.enums.YesNoEnum;
import com.wh.hotel.enums.base.HotelOperateTypeEnum;
import com.wh.hotel.mapper.HotelRoomTypeRelationMapper;
import com.wh.hotel.model.HotelRoomTypeRelation;
import com.wh.hotel.vo.PageVo;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author wenhao
 */
@Component
public class HotelRoomTypeRelationDao {

    @Resource
    private HotelRoomTypeRelationMapper hotelRoomTypeRelationMapper;

    @ResultLog(name = "HotelRoomTypeRelationDao.insert", methodType = MethodTypeEnum.DB)
    public boolean insert(HotelRoomTypeRelation hotelRoomTypeRelation) {
        return hotelRoomTypeRelationMapper.insert(hotelRoomTypeRelation) >= CommonConstant.SUCCESS_DB;
    }

    @ResultLog(name = "HotelRoomTypeRelationDao.updateByRoomTypeId", methodType = MethodTypeEnum.DB)
    public boolean updateByRoomTypeId(HotelRoomTypeRelation hotelRoomTypeRelation) {
        LambdaQueryWrapper<HotelRoomTypeRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HotelRoomTypeRelation::getRoomTypeId, hotelRoomTypeRelation.getRoomTypeId());
        return hotelRoomTypeRelationMapper.update(hotelRoomTypeRelation, queryWrapper) >= CommonConstant.SUCCESS_DB;
    }

    @ResultLog(name = "HotelRoomTypeRelationDao.selectPageByHotelId", methodType = MethodTypeEnum.DB, printResult = false)
    public IPage<HotelRoomTypeRelation> selectPageByHotelId(Long hotelId, PageVo pageVo) {
        IPage<HotelRoomTypeRelation> page = new Page<>(pageVo.getPageNum(), pageVo.getPageSize());
        LambdaQueryWrapper<HotelRoomTypeRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HotelRoomTypeRelation::getHotelId, hotelId);
        queryWrapper.orderByDesc(HotelRoomTypeRelation::getUpdateDt);
        return hotelRoomTypeRelationMapper.selectPage(page, queryWrapper);
    }

    @ResultLog(name = "HotelRoomTypeRelationDao.selectAllByHotelId", methodType = MethodTypeEnum.DB, printResult = false)
    public List<HotelRoomTypeRelation> selectAllByHotelId(Long hotelId, YesNoEnum yesNoEnum) {
        LambdaQueryWrapper<HotelRoomTypeRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HotelRoomTypeRelation::getHotelId, hotelId);
        queryWrapper.eq(HotelRoomTypeRelation::getRelationStatus, yesNoEnum.getCode());
        return hotelRoomTypeRelationMapper.selectList(queryWrapper);
    }


    @ResultLog(name = "HotelRoomTypeRelationDao.updateRelationStatus", methodType = MethodTypeEnum.DB)
    public boolean updateRelationStatus(Long id, HotelOperateTypeEnum operateTypeEnum) {
        HotelRoomTypeRelation updateModel = new HotelRoomTypeRelation();
        LambdaQueryWrapper<HotelRoomTypeRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HotelRoomTypeRelation::getRoomTypeId, id);
        switch (operateTypeEnum) {
            case ON:
                queryWrapper.in(HotelRoomTypeRelation::getRelationStatus, YesNoEnum.NO.getCode());
                updateModel.setRelationStatus(YesNoEnum.YES.getCode());
                break;
            case OFF:
                queryWrapper.eq(HotelRoomTypeRelation::getRelationStatus, YesNoEnum.YES.getCode());
                updateModel.setRelationStatus(YesNoEnum.NO.getCode());
                break;
        }
        return hotelRoomTypeRelationMapper.update(updateModel, queryWrapper) >= CommonConstant.SUCCESS_DB;
    }

    @ResultLog(name = "HotelRoomTypeRelationDao.selectByRoomTypeId", methodType = MethodTypeEnum.DB, printResult = false)
    public List<HotelRoomTypeRelation> selectByRoomTypeId(Long roomTypeId) {
        LambdaQueryWrapper<HotelRoomTypeRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HotelRoomTypeRelation::getRoomTypeId, roomTypeId);
        return hotelRoomTypeRelationMapper.selectList(queryWrapper);
    }

    @ResultLog(name = "HotelRoomTypeRelationDao.selectByRoomTypeIdList", methodType = MethodTypeEnum.DB, printResult = false)
    public List<HotelRoomTypeRelation> selectByRoomTypeIdList(List<Long> roomTypeIdList) {
        LambdaQueryWrapper<HotelRoomTypeRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(HotelRoomTypeRelation::getRoomTypeId, roomTypeIdList);
        return hotelRoomTypeRelationMapper.selectList(queryWrapper);
    }
}
